//
//  LoginViewController.m
//  UI14_豆瓣完整版
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LoginViewController.h"
#import "LTView.h"
#import "LandingViewController.h"
#import "BYJDataBaseHandle.h"
#import "UserModel.h"

@interface LoginViewController ()

@property (nonatomic, retain) LTView *name;

@end

@implementation LoginViewController

- (void)dealloc {
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"用户注册";
    
    self.name = [[LTView alloc] initWithFrame:CGRectMake(20, 20, 300, 50)];
    _name.myLabel.text = @"用户名";
    _name.myTextField.placeholder = @"请输入用户名";
    [self.view addSubview:_name];
    [_name release];
    
    LTView *code = [[LTView alloc] initWithFrame:CGRectMake(20, 80, 300, 50)];
    code.myLabel.text = @"密码";
    code.myTextField.placeholder = @"请输入密码";
    [self.view addSubview:code];
    [code release];
    
    LTView *code1 = [[LTView alloc] initWithFrame:CGRectMake(20, 140, 300, 50)];
    code1.myLabel.text = @"确认密码";
    code1.myTextField.placeholder = @"请输入密码";
    [self.view addSubview:code1];
    [code1 release];
    
    LTView *email = [[LTView alloc] initWithFrame:CGRectMake(20, 200, 300, 50)];
    email.myLabel.text = @"邮箱";
    email.myTextField.placeholder = @"请输入邮箱";
    [self.view addSubview:email];
    [email release];
    
    LTView *phone = [[LTView alloc] initWithFrame:CGRectMake(20, 260, 300, 50)];
    phone.myLabel.text = @"电话";
    phone.myTextField.placeholder = @"请输入电话";
    [self.view addSubview:phone];
    [phone release];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStyleDone target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 330, 100, 50)];
    button1.backgroundColor = [UIColor magentaColor];
    [button1 setTitle:@"确定" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(200, 330, 100, 50)];
    button2.backgroundColor = [UIColor cyanColor];
    [button2 setTitle:@"取消" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
}

- (void)button2Action:(UIButton *)button {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)button1Action:(UIButton *)button {
    
    // 查询数据库
    NSMutableArray *arr = [[BYJDataBaseHandle sharedDataBaseHandle] selectUserTable];
    NSMutableArray *modelArr = [NSMutableArray array];
    
    for (UserModel *model in arr) {
        NSString *str = model.name;
        [modelArr addObject:str];
    }
    
    if (![modelArr containsObject:_name.myTextField.text]) {
        
        // 插入数据
        UserModel *model = [UserModel userModel];
        model.name = _name.myTextField.text;
        [[BYJDataBaseHandle sharedDataBaseHandle] insertUserModel:model];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    } else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经注册过" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    }

}

- (void)itemAction:(UIBarButtonItem *)item {
    
    [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
